<template>
  <div class="user">
    <el-form
      ref="ruleForm"
      :rules="rules"
      :model="ruleForm"
      label-position="left"
       label-width="90px" 
    >
      <el-form-item label="旧密码：" prop="oldPassword">
        <el-input v-model="ruleForm.oldPassword"></el-input>
      </el-form-item>
      <el-form-item label="新密码：" prop="newPassword">
        <el-input v-model="ruleForm.newPassword"></el-input>
      </el-form-item>
      <el-form-item label="确认密码：" prop="confirmPassword">
        <el-input v-model="ruleForm.confirmPassword"> </el-input>
      </el-form-item>
    </el-form>
    <div>
      <el-button class="preservation" @click="submit()" type="primary"
        >保存</el-button
      >
    </div>
  </div>
</template>

<script>
import { mapGetters, mapActions, mapMutations } from "vuex";
export default {
  components: {},
  data() {
    return {
      ruleForm: {
        oldPassword: "",
        newPassword: "",
        confirmPassword: "",
      },
      rules: {
        oldPassword: [
          { required: true, message: "请输入旧密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
        ],
        newPassword: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
        ],
        confirmPassword: [
          { required: true, message: "请确认新密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {},
  methods: {
    ...mapActions(["changePassword"]),
    submit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.ruleForm.newPassword != this.ruleForm.confirmPassword) {
            this.$message.error("两次新密码不一致");
          } else {
            let data = {
              old_password: this.ruleForm.oldPassword,
              new_password: this.ruleForm.newPassword,
            };
            this.changePassword(data).then((res) => {
              this.$message({
                message: "修改成功",
                type: "success",
              });
              this.$refs.ruleForm.resetFields();
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  computed: {},
};
</script>

<style scoped>
.user {
  padding: 15px 5%;
  background: #fff;
  min-height: 50vh;
  box-sizing: border-box;
  width: 100%;
}
.user >>> .el-form-item__label {
  padding-right: 0;
}
.user >>> .el-form-item {
  border-bottom: 1px solid #e6e6e6;
  padding-bottom: 10px;
  margin-bottom: 10px;
}
.user >>> .el-input__inner {
  border: none;
}
.preservation {
  width: 100%;
  height: 45px;
  background: #0e93e4;
  border-radius: 3px;
  font-size: 18px;
  font-family: Source Han Sans CN;
  font-weight: 500;
  color: #ffffff;
  margin-top: 30px;
  margin-bottom: 60px;
}
</style>